package com.fisher.checker;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.EditText;

public class PrivateKeyInputDialog {

    public interface PrivateKeyListener {
        void onPrivateKeyEntered(String privateKey);

        void cancle();
    }

    public static void showPrivateKeyInputDialog(Context context, final PrivateKeyListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("请输入激活码");

        // Set up the input
        final EditText input = new EditText(context);
        input.setMaxLines(1);

        builder.setView(input);
        input.requestFocus();

        // Set up the buttons
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String privateKey = input.getText().toString();
                privateKey = privateKey.replace("\n","");
                if (listener != null) {
                    listener.onPrivateKeyEntered(privateKey);
                }
                input.clearFocus();
                dialog.dismiss();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (listener != null) {
                    listener.cancle();
                }
                input.clearFocus();
                input.setFocusable(false);
                dialog.dismiss();
            }
        });

        builder.show();
    }
}

